package com.zhongkai.service.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.mysql.jdbc.StringUtils;
import com.zhongkai.app.service.SendSmsService;
import com.zhongkai.jedis.JedisClient;
import com.zhongkai.model.utils.ZhongkaiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.regex.Pattern;

/**
 * 短信发送
 */

@Service
@Transactional(rollbackFor = Exception.class)
public class SendSmsServiceImpl implements SendSmsService {

    @Autowired
    private JedisClient jedisClient;

    @Override
    public ZhongkaiResult send(String phone) {
        if (StringUtils.isNullOrEmpty(phone)) {
            return ZhongkaiResult.build(500,"请输入手机号");
        }

        String prc = "^((13[0-9])|(14[5-9])|(15([0-3]|[5-9]))|(16([5,6])|(17[0-8])|(18[0-9]))|(19[1,8,9]))\\d{8}$";


        if (!Pattern.matches(prc, phone.trim())) { //正则验证1.0.1的格式

            return ZhongkaiResult.build(500, "手机号格式不正确");
        }

        String code  = null;
        try {
            code = jedisClient.get(phone);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(500, "redis连接获取失败");
        }

        if (!StringUtils.isNullOrEmpty(code)){
            return ZhongkaiResult.build(500,"验证码未过期");
        }

        String verificationCode = String.valueOf((int)((Math.random()*9+1)*1000));

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4FofXveecEt1TFgjPe6C", "biziYZmbtqRDQGa8mkQR5VHBO4zeVz");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");

/*        DateTime dateTime = new DateTime();
        long millis = dateTime.getMillis(); //获取当前系统时间戳*/
        request.setVersion("2017-05-25");


        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "中凯智能");
        request.putQueryParameter("TemplateCode", "SMS_133025067");

       // request.putQueryParameter("TemplateParam", "{\"code\":\"10086\"}");
        request.putQueryParameter("TemplateParam", "{\"code\":\""+verificationCode+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

        try {
            jedisClient.set(phone,verificationCode);
            jedisClient.expire(phone, 300);//设置过期时间

        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(500, "redis连接获取失败");
        }
        return ZhongkaiResult.ok();
    }
}
